package com.koron.order.config;

import com.koron.order.change.util.ThreadLocalOrderChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName ChangeControllerInterceptor
 * @description
 * @Author zhouj
 * @Date 2022/6/1 16:32
 */
@Slf4j
@Component
public class ChangeControllerInterceptor implements HandlerInterceptor {

    /**
     * 由于只有工单变更需要上下文提供标识 只需要配置拦截路径/orderChange/**
     * 在controller调用之前执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        log.info("工单变更获取当前变更标识:{}", ThreadLocalOrderChange.get());
        return true;
    }

    /**
     * 在controller调用中执行
     */
    @Override
    public void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {
    }

    /**
     * 在controller调用后执行
     */
    @Override
    public void afterCompletion(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        log.info("工单变更销毁变更标识:{}", ThreadLocalOrderChange.get());
        ThreadLocalOrderChange.clear();
    }

}
